package com.zh.servicer.service.impl;


import com.zh.common.utils.MD5Utils;
import com.zh.common.utils.StringUtil;
import com.zh.servicer.dao.ServiceDao;
import com.zh.servicer.dao.ServiceRoleDao;
import com.zh.servicer.domain.RoleUserIdDO;
import com.zh.servicer.domain.ServiceDO;
import com.zh.servicer.domain.ServiceRoleDO;
import com.zh.servicer.service.ServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class ServiceServiceImpl implements ServiceService {
	@Autowired
	private ServiceDao serviceDao;
	@Autowired
	private ServiceRoleDao serviceRoleDao;
	
	@Override
	public ServiceDO get(String userId){
		return serviceDao.get(userId);
	}
	
	@Override
	public List<ServiceDO> list(Map<String, Object> map){
		return serviceDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return serviceDao.count(map);
	}

	/*@Transactional
	@Override
	public int save(ServiceDO service) {
		int count = serviceDao.save(service);
		String userId = service.getUserId();
		List<String> roles = service.getRoleIds();
		serviceRoleDao.removeByUserId(userId);
		List<ServiceRoleDO> list = new ArrayList<>();
		for (String roleId : roles) {
			ServiceRoleDO ur = new ServiceRoleDO();
			ur.setId(StringUtil.generalKeyword());
			ur.setUserId(userId);
			ur.setRoleId(roleId);
			list.add(ur);
		}
		if (list.size() > 0) {
			serviceRoleDao.batchSave(list);
		}
		return count;
	}*/

	@Override
	public int save(ServiceDO staff) {
		staff.setPassword(MD5Utils.encrypt(staff.getUsername(),staff.getPassword()));
		staff.setUserId(StringUtil.generalKeyword());
		int count = serviceDao.save(staff);
		String userId =staff.getUserId();
		List<String> roles = staff.getRoleIds();
		serviceRoleDao.removeByUserId(userId);
		List<RoleUserIdDO> list = new ArrayList<>();
		for (String roleId : roles) {
			RoleUserIdDO sr = new RoleUserIdDO();
			sr.setId(StringUtil.generalKeyword());
			sr.setUserId(userId);
			sr.setRoleId(roleId);
			list.add(sr);
		}
		if (list.size() > 0) {
			serviceRoleDao.batchSave(list);
		}
		return count;
	}

	//保存服务商人员与角色
	//@Transactional
	@Override
	public int saveServicerStaff(ServiceDO serviceDO) {

		serviceDO.setUserId(StringUtil.generalKeyword());
		serviceDO.setPassword(MD5Utils.encrypt(serviceDO.getUsername(), serviceDO.getPassword()));
		serviceDO.setStatus(1);
		int count = serviceDao.save(serviceDO);
		String userId =serviceDO.getUserId();
		List<String> roles = serviceDO.getRoleIds();
		serviceRoleDao.removeByUserId(userId);
		List<RoleUserIdDO> list = new ArrayList<>();
		for (String roleId : roles) {
			RoleUserIdDO sr = new RoleUserIdDO();
			sr.setId(StringUtil.generalKeyword());
			sr.setUserId(userId);
			sr.setRoleId(roleId);
			list.add(sr);
		}
		if (list.size() > 0) {
			serviceRoleDao.batchSave(list);
		}
		return count;
	}


	@Override
	public int update(ServiceDO service){
		return serviceDao.update(service);
	}
	
	@Override
	public int remove(String userId){
		return serviceDao.remove(userId);
	}
	
	@Override
	public int batchRemove(String[] userIds){
		return serviceDao.batchRemove(userIds);
	}

	@Override
	public List<ServiceDO> type() {
		return serviceDao.type();
	}

}
